package com.wechat.service;

import com.wechat.dao.*;
import com.wechat.dao.extend.AnswerDetailExtDao;
import com.wechat.dao.extend.AnswerExtDao;
import com.wechat.dao.extend.IReceivedDao;
import com.wechat.dao.extend.TestRecordDao;
import com.wechat.entity.Question;
import com.wechat.entity.QuestionDetail;
import com.wechat.entity.QuestionLib;
import com.wechat.entity.User;
import com.wechat.entity.extend.AnswerDetailExt;
import com.wechat.entity.extend.AnswerExt;
import com.wechat.entity.extend.IReceived;
import com.wechat.entity.extend.TestRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ycx on 2018/4/13.
 */
@Service
public class RecordService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AnswerDao answerDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private IReceivedDao receivedDao;
    @Autowired
    private TestRecordDao testRecordDao;
    @Autowired
    private QuestionLibDao questionLibDao;
    @Autowired
    private AnswerExtDao answerExtDao;
    @Autowired
    private QuestionDetailDao questionDetailDao;
    @Autowired
    private AnswerDetailExtDao answerDetailExtDao;

    public List<QuestionLib> findQuestionLibList(Integer questionId) {
        List<QuestionLib> questionLibList = questionLibDao.selQuestionList(questionId);
        return questionLibList;
    }

    /**
     * 批量保存出题记录明细
     *
     * @param records
     * @param question
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer batchSaveQuestionDetail(List<QuestionDetail> records, Question question) {
        questionDao.save(question);

        for (int i = 0; i < records.size(); i++) {
            QuestionDetail record = records.get(i);
            record.setId(null);
            record.setUserId(question.getUserId());
            record.setQuestionId(question.getId());
            record.setCreateTime(null);
        }

        questionDetailDao.batchSave(records);
        return question.getId();
    }

    public List<QuestionDetail> selQuestionDetailsByParam(QuestionDetail record) {
        List<QuestionDetail> questionDetailList = questionDetailDao.selByParam(record);
        return questionDetailList;
    }

    public List<AnswerDetailExt> findAnswerDetailExt(Integer answerId) {
        List<AnswerDetailExt> result = answerDetailExtDao.findByAnswerId(answerId);
        return result;
    }

    /**
     * 根据传入的用户编号查询我发出去的  红包总数与红包金额总数 分别存放于 total_number与total_amount
     *
     * @param userId
     * @return
     */
    public Question selTotalSend(Integer userId) {
        Question question = questionDao.selTotalSend(userId);
        return question;
    }

    /**
     * 根据用户编号查询用户信息，取得昵称
     *
     * @param userId
     * @return
     */
    public User getUserById(Integer userId) {
        User result = userDao.findById(userId);
        return result;
    }

    /**
     * 根据用户编号查询出题记录
     *
     * @return
     */
    public List<TestRecord> selTestRecord(Integer userId,Integer questionState) {
        List<TestRecord> result = testRecordDao.selTestRecord(userId,questionState);
        return result;
    }

    /**
     * 根据传入的用户编号查询我收到页面的相关信息
     *
     * @param userId
     * @return
     */
    public IReceived selIReceivedByUserId(Integer userId) {
        IReceived result = receivedDao.selByUserId(userId);
        return result;
    }

    /**
     * 根据用户id查询用户答题列表
     *
     * @param userId
     * @return
     */
    public List<AnswerExt> selAnswerRecordsByUserId(Integer userId) {
        List<AnswerExt> result = answerExtDao.selAnswerRecordsByUserId(userId);
        return result;
    }

    /**
     * 通过用户id查询获得的总红包金额
     *
     * @param userId
     * @return
     */
    public double getSumRedPacketByUserId(Integer userId) {
        double result = answerDao.selSumRedPacketByUserId(userId);
        return result;
    }

    /**
     * 通过用户id查询获得的总红包个数
     *
     * @param userId
     * @return
     */
    public int getCountRedPacketByUserId(Integer userId) {
        int result = answerDao.selCountRedPacketByUserId(userId);
        return result;
    }

    /**
     * 通过用户id查询我发出去的红包总金额
     *
     * @param userId
     * @return
     */
    public double sendSumRedPacketByUserId(Integer userId) {
        double result = questionDao.selSumRedPacketByUserId(userId);
        return result;
    }

    /**
     * 通过用户id查询我发出去的红包总个数
     *
     * @param userId
     * @return
     */
    public int sendCountRedPacketByUserId(Integer userId) {
        int result = questionDao.selCountRedPacketByUserId(userId);
        return result;
    }

}



























